package com.zzyl.nursing.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.iot20180120.Client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.framework.config.properties.AliIoTConfigProperties;
import com.zzyl.nursing.domain.Content;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDataPageReqDto;
import com.zzyl.nursing.mapper.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.service.IDeviceDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 设备数据Service业务层处理
 * 
 * @author lww
 * @date 2024-11-19
 */
@Service
public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements IDeviceDataService
{
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private AliIoTConfigProperties aliIoTConfigProperties;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询设备数据
     * 
     * @param id 设备数据主键
     * @return 设备数据
     */
    @Override
    public DeviceData selectDeviceDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询设备数据列表
     * 
     * @param deviceData 设备数据
     * @return 设备数据
     */
    @Override
    public List<DeviceData> selectDeviceDataList(DeviceData deviceData){
        return deviceDataMapper.selectDeviceDataList(deviceData);
    }

    /**
     * 新增设备数据
     * 
     * @param deviceData 设备数据
     * @return 结果
     */
    @Override
    public int insertDeviceData(DeviceData deviceData)
    {
        return save(deviceData) ? 1 : 0;
    }

    /**
     * 修改设备数据
     * 
     * @param deviceData 设备数据
     * @return 结果
     */
    @Override
    public int updateDeviceData(DeviceData deviceData)
    {
        return updateById(deviceData) ? 1 : 0;
    }

    /**
     * 批量删除设备数据
     * 
     * @param ids 需要删除的设备数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除设备数据信息
     * 
     * @param id 设备数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 批量插入设备数据
     * @param content
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsertDeviceData(Content content) {
        //如果没有该设备则结束
        Device device = deviceMapper.selectOne(new LambdaQueryWrapper<Device>().eq(Device::getIotId, content.getIotId()));

        if (ObjectUtil.hasNull(device)){
            return;
        }
        //批量构建设备数据
        ArrayList<DeviceData> deviceDataArrayList = new ArrayList<>();

        content.getItems().forEach((k,v)->{
            DeviceData deviceData = DeviceData.builder()
                    .functionId(k)
                    .dataValue(StrUtil.toString(v.getValue()))
                    .alarmTime(LocalDateTimeUtil.of(v.getTime()))
                    .accessLocation(device.getRemark())
                    .build();

            BeanUtil.copyProperties(device,deviceData, CopyOptions.create().ignoreNullValue());
            deviceData.setId(null);

            deviceDataArrayList.add(deviceData);
        });

        saveBatch(deviceDataArrayList);
        //将数据存入redis，方便后面智能床位获取最新的数据
        redisCache.setCacheMapValue(CacheConstants.IOT_DEVICE_LIST_DATA, device.getIotId(), deviceDataArrayList);
    }




    @Override
    public TableDataInfo queryPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        Page page = Page.of(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        lambdaQuery()
                .like(StrUtil.isNotBlank(deviceDataPageReqDto.getDeviceName()), DeviceData::getDeviceName, deviceDataPageReqDto.getDeviceName())
                .eq(StrUtil.isNotBlank(deviceDataPageReqDto.getFunctionId()), DeviceData::getFunctionId, deviceDataPageReqDto.getFunctionId())
                .ge(ObjectUtil.isNotNull(deviceDataPageReqDto.getStartTime()), DeviceData::getAlarmTime, deviceDataPageReqDto.getStartTime())
            .le(ObjectUtil.isNotNull(deviceDataPageReqDto.getEndTime()), DeviceData::getAlarmTime, deviceDataPageReqDto.getEndTime())
            .page(page);
        TableDataInfo<List<DeviceData>> tableDataInfo = new TableDataInfo<>();

        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getRecords());

        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;

    }


}
